﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDF.Models;
using PetaPoco;

namespace SDF.MvcApp.Areas.Backend.Controllers
{
    public class AuthController : ControllerBase
    {

        public List<SelectListItem> StatusItems = new List<SelectListItem> {
                new SelectListItem { Text = "启用", Value = "1" },
                new SelectListItem { Text = "禁用", Value = "0" } 
        };
        //
        // GET: /Backend/Auth/

        public ActionResult Index(int pageNum = 1, int numPerPage = 10)
        {
            //List<AuthGroup> list = db.Fetch<AuthGroup>("where Id>0");
            Page<AuthGroup> page = db.Page<AuthGroup>(pageNum, numPerPage, "where id>0 order by id desc");
            return View(page);
        }



        //
        // GET: /Backend/Auth/Create

        public ActionResult CreateGroup()
        {
            ViewBag.Status = this.StatusItems;
            return View();
        }

        //
        // POST: /Backend/Auth/Create

        [HttpPost]
        public ActionResult CreateGroup(AuthGroup model)
        {
            try
            {
                model.Insert();
                return NavTabId("AuthIndex");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Backend/Auth/Edit/5

        public ActionResult EditGroup(int id)
        {
            var model = db.Single<AuthGroup>(id);
            ViewBag.Status = new SelectList(this.StatusItems, "Value", "Text", model.Status);
            return View(model);
        }

        //
        // POST: /Backend/Auth/Edit/5

        [HttpPost]
        public ActionResult EditGroup(int id, AuthGroup newModel)
        {
            try
            {
                var model = db.SingleOrDefault<AuthGroup>(id);
                model.Title = newModel.Title;
                model.Status = newModel.Status;
                model.Remark = newModel.Remark;
                model.Save();
                return NavTabId("AuthIndex");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Backend/Auth/Delete/5

        public ActionResult DelGroup(int id)
        {
            db.Delete<AuthGroup>("where id=@0", id);
            return NavTabId("AuthIndex", false);
        }




    }
}
